package cbfg.rvadapter.demo.state2

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cbfg.rvadapter.RVHolder
import cbfg.rvadapter.RVHolderFactory
import cbfg.rvadapter.demo.databinding.LayoutStateEmptyBinding
import cbfg.rvadapter.demo.databinding.LayoutStateLoadErrorBinding
import cbfg.rvadapter.demo.databinding.LayoutStateLoadMoreBinding
import cbfg.rvadapter.demo.databinding.LayoutStateRefreshingBinding
import cbfg.rvadapter.state.RVState
import cbfg.rvadapter.state.RVStateFlags

/**
 * @author: TomHawk
 * @description:
 * @date: 2025/6/30 16:23
 */
class State2VHF : RVHolderFactory() {
    override fun getItemViewType(position: Int, item: Any): Int {
        item as RVState
        return item.state
    }

    override fun createViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup?,
        viewType: Int,
        itemClazz: Class<*>?
    ): RVHolder<out Any> {
        return when (viewType) {
            RVStateFlags.REFRESHING -> {
                RefreshingVH(LayoutStateRefreshingBinding.inflate(inflater, parent, false))
            }

            RVStateFlags.LOAD_MORE -> {
                LoadMoreVH(LayoutStateLoadMoreBinding.inflate(inflater, parent, false))
            }

            RVStateFlags.LOAD_MORE_ERROR -> {
                LoadMoreErrorVH(LayoutStateLoadErrorBinding.inflate(inflater, parent, false))
            }

            RVStateFlags.EMPTY -> {
                EmptyVH(LayoutStateEmptyBinding.inflate(inflater, parent, false))
            }

            else -> throw IllegalArgumentException("unsupported viewType = $viewType")
        }
    }

    private class RefreshingVH(
        binding: LayoutStateRefreshingBinding
    ) : RVHolder<RVState>(binding.root) {
        override fun setContent(item: RVState, payload: Any?) {}
        override fun setListeners(
            itemClickListener: View.OnClickListener?,
            itemLongClickListener: View.OnLongClickListener?
        ) {
        }
    }

    private class LoadMoreVH(
        binding: LayoutStateLoadMoreBinding
    ) : RVHolder<RVState>(binding.root) {
        override fun setContent(item: RVState, payload: Any?) {}
        override fun setListeners(
            itemClickListener: View.OnClickListener?,
            itemLongClickListener: View.OnLongClickListener?
        ) {
        }
    }

    private class LoadMoreErrorVH(
        private val binding: LayoutStateLoadErrorBinding
    ) : RVHolder<RVState>(binding.root) {
        override fun setContent(item: RVState, payload: Any?) {}
        override fun setListeners(
            itemClickListener: View.OnClickListener?,
            itemLongClickListener: View.OnLongClickListener?
        ) {
            binding.btnRetry.setOnClickListener(itemClickListener)
        }
    }

    private class EmptyVH(
        binding: LayoutStateEmptyBinding
    ) : RVHolder<RVState>(binding.root) {
        override fun setContent(item: RVState, payload: Any?) {}
        override fun setListeners(
            itemClickListener: View.OnClickListener?,
            itemLongClickListener: View.OnLongClickListener?
        ) {
        }
    }
}